#!/bin/bash
set -e
app="${moduleName}"
logdir="${logDir}"
graceperiod=${checkGracePeriod}
successflag="BCS_MODULE_SUCCESS"
failureflag="BCS_MODULE_FAILURE"

exit 0

if [[ ! -f "pid/${app}.pid" ]];then
	echo "pid/${app}.pid not exist"
	exit 1
fi

# pid store in file
fpid=$(cat "pid/${app}.pid")
if [[ -z fpid ]];then
	echo "pid is null"
	exit 1
fi

if ! ps -p $fpid > /dev/null;then
    echo "pid ${fpid} is not running" 
    exit 1
fi

# pid grep from running process
rpid=$(pidof "$app")

if [[ "$rpid" == "" ]];then
    echo "$app is not running"
    exit 1
fi

for try in `seq ${graceperiod}`; do
	check=$(cat "$logdir/$app.log.INFO" | grep "BCS_CHECK")

	if [[ $check == *"${fpid}"*"${successflag}"* ]];then
		echo "$app is working successful"
		exit 0

	if [[ $check == *"${fpid}"*"${failureflag}"* ]];then
		echo "$app is not working"
		exit 1

	sleep 1
done

echo "$app is not working"
exit 1
